package com.fred.colorpicker

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class ColorPickerMenu() {
    private lateinit var showDialog: MutableState<Boolean>
    private lateinit var animatedState: MutableTransitionState<Boolean>
    private lateinit var colorCallback:MutableState<((Int?, Int?) -> Unit)?>
    private val colorChanged = { rectColor:Int?, ringColor:Int?->
        println("rectColor:${rectColor}  ringColor:${ringColor}")
    }

    @Composable
    private fun InitState() {
        showDialog = remember { mutableStateOf(false) }
        animatedState = remember {
            MutableTransitionState(false).apply {
                targetState = true
            }
        }
        colorCallback = remember {
            mutableStateOf(colorChanged)
        }
    }

    @Composable
    fun InitColorPickerMenu() {
        InitState()
        if (showDialog.value) {
            println("showDialog 重组")
            Column(modifier = Modifier.fillMaxSize()) {
                val interactionSource = remember { MutableInteractionSource() }
                Box(modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f)
                    .clickable(interactionSource = interactionSource, indication = null, onClick = {
                        dismiss()
                    })
                    .background(Color.Transparent))
                AnimatedVisibility(visibleState = animatedState, modifier = Modifier.fillMaxWidth(),
                                   enter = slideInVertically(
                                       animationSpec = tween(durationMillis = 300,
                                                             easing = LinearOutSlowInEasing),
                                       initialOffsetY = { fullHeight ->
                                           fullHeight
                                       }), exit = slideOutVertically(
                        animationSpec = tween(durationMillis = 300, easing = FastOutLinearInEasing),
                        targetOffsetY = { fullHeight ->
                            fullHeight
                        })) {
                    ColorPickerBody()
                }
            }
            if (animatedState.isIdle) {
                if (!animatedState.targetState) {
                    showDialog.value = false
                }
            }
        }
    }

    @Composable
    private fun ColorPickerBody() {
        Column(modifier = Modifier
            .background(color = Color.Black)
            .padding(16.dp)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "拖动", color = Color.White,
                     fontSize = 16.sp,modifier = Modifier.padding(8.dp))
                Image(painter = painterResource(id = R.drawable.ic_rect), contentDescription = "")
                Text(text = "和", color = Color.White,
                     fontSize = 16.sp,modifier = Modifier.padding(8.dp))
                Image(painter = painterResource(id = R.drawable.ic_circle), contentDescription = "")
                Text(text = "调整背景和文字颜色", color = Color.White,
                     fontSize = 16.sp,modifier = Modifier.padding(8.dp))
            }
            Divider(modifier = Modifier
                .fillMaxWidth()
                .height(8.dp))
            ColorPicker(colorChanged = colorCallback.value)
        }
    }

    fun show() {
        colorCallback.value = colorChanged
        showDialog.value = true
        animatedState.targetState = true
    }

    private fun dismiss() {
        colorCallback.value = null
        animatedState.targetState = false
    }
}